
/* Blinker Demo */

/* Include useful pre-defined functions */
#include <avr/interrupt.h>    // Defines pins, ports, etc to make programs easier to read
#include <avr/io.h>    // header file
#define F_CPU 8000000UL	      // Sets up the default speed for delay.h
#include <util/delay.h>
#include <inttypes.h>
#include <avr/sleep.h>
#include "iocompat.h"
#include "USI_TWI_Master.h"


/// Typedefs //////////
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;


void InitUSART (u16 baud)
{
    // Set Baud rate - Cast High byte
    UBRRH = (u8)(baud>>8);
    // Set Baud rate - Cast Low byte                       
    UBRRL = (u8)baud;                            
    // Enable Receiver & Transmitter
    UCSRB = (1<<RXEN)|(1<<TXEN);                 
    // 0 parity, 1 stop bit
    UCSRC = (1<<USBS)|(3<<UCSZ0);    
} 

void TxByte (u8 data)
{
   // Wait for empty transmit buffer
   while ( !( UCSRA & (1<<UDRE)) );              
   // Putting data into the buffer, forces transmission
   UDR = data;                                   
}

u8 RxByte (void)
{
   // Wait for data to be received
   while ( !(UCSRA & (1<<RXC)) );                
   // Return Data
   return UDR;                                   
} 

void print(char *str)
{
	int i=0;
	while (str[i]!=0){
		TxByte(str[i]);
		i++;
	}
}

void sleep(float msPeriod){
	while(msPeriod>10){
		_delay_ms(10);
		msPeriod-=10;
	}
	_delay_ms(msPeriod);
}

enum { UP, DOWN };

//ISR (TIMER1_OVF_vect)           /* Note [2] */
//{
//	static uint16_t pwm;        /* Note [3] */
//    static uint8_t direction;

//    switch (direction)          /* Note [4] */
//    {
//            if (++pwm == TIMER1_TOP)
//               direction = DOWN;
//            break;
//
//        case DOWN:
//            if (--pwm == 0)
//                direction = UP;
//            break;
//    }

    //OCR = pwm;                /* Note [5] */
//}

void ioinit (void)                   /* Note [6] */
{
    /* Timer 1 is 10-bit PWM (8-bit PWM on some ATtinys). */
    TCCR1A = TIMER1_PWM_INIT;
    /*
     * Start timer 1.
     *
     * NB: TCCR1A and TCCR1B could actually be the same register, so
     * take care to not clobber it.
     */
    TCCR1B |= TIMER1_CLOCKSOURCE;
    /*
     * Run any device-dependent timer 1 setup hook if present.
     */
#if defined(TIMER1_SETUP_HOOK)
    TIMER1_SETUP_HOOK();
#endif

    /* Set PWM value to 0. */
    OCR = 0;

    /* Enable OC1 as output. */
    DDROC = _BV (OC1);

    /* Enable timer 1 overflow interrupt. */
    //TIMSK = _BV (TOIE1);
    sei ();
}



int main(void){


  // Initialize USART 9600 baud
  ioinit ();
   // for (;;)                    /* Note [7] */
   //     sleep_mode();

InitUSART(51);
DDRD = 255;	
PORTD = 0; 
print("\n\nInitializing...\n");
sleep(2000);
PORTD = 255;
print("Running.\n");

while (1)
{
	char str[20]=">";
	
	print(str);
	u8 rcv=RxByte();

	if (rcv>='0' && rcv<='9'){
		//TxByte(rcv-'0');
		PORTD = ~_BV(rcv-'0');
		OCR = (rcv-'0')*100;
	}
	
    TxByte(rcv);
	TxByte('\n');
} 
  DDRD = 255;		      /* enable output on port D, pin 4 */
	
  PORTD = 255; 
  sleep(2000);

  while(1){

	PORTD = ~_BV(PD1); 
    _delay_ms(1000);
	PORTD = ~_BV(PD2); 
    _delay_ms(1000);
    PORTD = ~_BV(PD3); 
    _delay_ms(1000);
	PORTD = ~_BV(PD4); 
    _delay_ms(1000);
	PORTD = ~_BV(PD5); 
	_delay_ms(1000);


  }

  return(0);
}

